import 'dart:async';
import 'dart:ui';

import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter/services.dart';
import 'package:flutter_rock_ui/extensions/app_extensions.dart';
import 'package:flutter_rock_ui/widget_flutter_binding/screen_adapter_binding.dart';
import 'package:flutter_rock_ui/widget_flutter_binding/ui_blueprints_rectangle.dart';
import 'package:get/get.dart';
import 'package:trip_tales/app/app_bindings.dart';
import 'package:trip_tales/config/config.dart';
import 'package:trip_tales/routes/app_pages.dart';
import 'package:trip_tales/theme/dark/dark_theme.dart';
import 'package:trip_tales/theme/light/light_theme.dart';

var uiSize = const BlueprintsRectangle(Config.baseWidth, Config.baseHeight);

void main() {
  realRunApp();
}

void realRunApp() {
  runZonedGuarded<Future<void>>(() async {
    debugPaintSizeEnabled = false;

    /// 屏幕适配&&powerImage
    FxWidgetsFlutterBinding.ensureInitialized(uiBlueprints: uiSize);

    runApp(const MyApp());

    setSystemUI();
  }, (e, s) {
    e.toString().toast();
    s.toString().toast();
  });
}

void setSystemUI() {
  if (GetPlatform.isAndroid) {
    /// 实现沉浸式状态栏
    SystemUiOverlayStyle systemUiOverlayStyle = const SystemUiOverlayStyle(statusBarColor: Colors.transparent);
    SystemChrome.setSystemUIOverlayStyle(systemUiOverlayStyle);
  } else if (GetPlatform.isIOS) {
    SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle.dark);
  }

  ///防止UI随着手机的旋转而在横竖屏间切换
  SystemChrome.setPreferredOrientations([
    DeviceOrientation.portraitUp,
    DeviceOrientation.portraitDown,
  ]);
  SystemChrome.setEnabledSystemUIMode(SystemUiMode.manual, overlays: [SystemUiOverlay.top, SystemUiOverlay.bottom]);
}

class MyApp extends StatefulWidget {
  const MyApp({super.key});

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp>with WidgetsBindingObserver {
  Brightness? _brightness;

  @override
  void initState() {
    super.initState();
    _brightness = PlatformDispatcher.instance.platformBrightness;
    WidgetsBinding.instance.addObserver(this);
  }

  @override
  void dispose() {
    WidgetsBinding.instance.removeObserver(this);
    super.dispose();
  }

  @override
  void didChangePlatformBrightness() {
    super.didChangePlatformBrightness();
    setState(() {
      _brightness = PlatformDispatcher.instance.platformBrightness;
    });
  }

  @override
  Widget build(BuildContext context) {
    return GetMaterialApp(
      theme: _brightness == Brightness.light ? LightTheme.lightTheme : DarkTheme.darkTheme,
      getPages: AppPages.getPages(),
      initialBinding: AppBinding(),
      // initialRoute: Routes.initial,
      defaultTransition: Transition.fade,
      builder: FxTransitionBuilder(builder: null),
      locale: const Locale('en', 'US'),

      /// 多语言
      // localizationsDelegates: const [
      //   GlobalMaterialLocalizations.delegate,
      //   GlobalWidgetsLocalizations.delegate,
      //   GlobalCupertinoLocalizations.delegate, //多加这行代码
      // ],
      supportedLocales: const [
        Locale('en', 'US'), // 美国英语
        Locale('zh', 'CN'), // 中文简体
        //其它Locales
      ],
    );
  }
}
